// pages/index/zone/goodsDetail/appraise/appraise.js
import { axios } from '../../../../utils/ajax.js'
import { checklogin } from '../../../../utils/login.js'
Page({

  /**ff
   * 页面的初始ff数据
   */
  data: {
    goodsId: '',
    //是否这个商品有晒单
    comment: '',
    //头部导航index
    titleindex:0,
    data:'',
    //当前页码
    page: 1,
    //总页码
    totalpages:'',
    //全部晒单数
    allNum:'',
    //本品晒单数
    oneNum: '',
    //能否加载更多状态
    canload: true,
    userid:'',
    token:'',
    //能否发布评价按钮
    canpublish: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    this.setData({
      userid: userid,
      token: token
    })

    //查看有无待评价商品
    if (userid){
      axios({
        url: '/bask/baskgoodslist',
        data: {
          userid: userid,
          token: token,
          goods_id: '',
          page: 1
        }
      }).then((res) => {
        console.log(res)
        this.setData({
          canpublish: res.data.availableCount?true:false
        })
      })
    }


    console.log(options)
    this.setData({
      goodsId: options.goodsId,
      comment: options.comment == 'false'?false:true
    })
    //拿到全部晒单数
    axios({
      url: '/bask/baskList',
      data: {
        type: 2,   //1：本品晒单，2：全部晒单
        page: 1,
        userid: this.data.userid,
        token: this.data.token
      }
    }).then(res => {
      this.setData({
        allNum: res.data.allNum
      })
    })


    //查看本品是否有晒单
    axios({
      url: '/bask/baskList',
      data: {
        type: 1,   //1：本品晒单，2：全部晒单
        page: 1,
        goodsId: this.data.goodsId,
        userid: this.data.userid,
        token: this.data.token
      }
    }).then(res => {
      console.log(res)
      //如果本品有数据
      if (res.data.data){
        this.getdata(1)
        this.setData({
          titleindex: 1
        })
      }else{
        this.getdata(0)
        this.setData({
          titleindex: 0
        })
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //nav切换
  choosenav (e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      titleindex: index,
      page: 1,
      canload: false
    })
    this.getdata(index)
  },
  //查看缩略图
  openimg (e) {
    let index = e.currentTarget.dataset.index
    let thumb = e.currentTarget.dataset.thumb
    let obj = this.data.data
    let result = obj[index].photos
    let previewImage = []
    previewImage = result.map(x => x.thumbUrl)
    wx.previewImage({
      current: thumb,
      urls: previewImage,
    })
  },
  //去商品详情页
  gogooddetail (e) {
    console.log(e.currentTarget.dataset)
    let { goodsid, goodsname} = e.currentTarget.dataset
    wx.navigateTo({
      url: `../goodsDetail?linkId=${goodsid}&title=${goodsname}`
    })
  },
  //点赞 取消点赞
  givelike (e) {
    //检查登录
    checklogin().then((res)=>{
      console.log(res)
      let islike = e.currentTarget.dataset.islike
      let boid = e.currentTarget.dataset.boid
      let like = e.currentTarget.dataset.like
      console.log(boid, islike,like)
      axios({
        url: '/bask/like',
        data: {
          userid: res.userid,
          token: res.token,
          boId: boid,
          type: islike ? 0 : 1
        }
      }).then((res)=>{
        console.log(res)
        let data = this.data.data
        data.find(x => x.boId == boid).isLike = !islike
        if (islike==1){
          data.find(x => x.boId == boid).like = parseInt(data.find(x => x.boId == boid).like) - 1
        }else{
          data.find(x => x.boId == boid).like = parseInt(data.find(x => x.boId == boid).like) + 1
        }
        this.setData({
          data: data
        })
      })
    })
  },
  //发布评价
  publish () {
    checklogin().then((res)=>{
      console.log(res)
      wx.navigateTo({
        url: './publish/publish',
      })
    })
  },
  //获取数据
  getdata (datatype,page=1,loadmore = false) {
    //datatype  0全部  1本品
    //查全部晒单
    if (datatype == 0){
      axios({
        url: '/bask/baskList',
        data: {
          type: 2,   //1：本品晒单，2：全部晒单
          page: page,
          userid: this.data.userid,
          token: this.data.token
        }
      }).then(res => {
        console.log(res)
        if (!loadmore){
          this.setData({
            titleindex: 0,
            data: res.data.data,
            totalpages: res.data.totalpages
          })
        }else{
          let data = this.data.data.concat(res.data.data)
          this.setData({
            titleindex: 0,
            data: data,
            totalpages: res.data.totalpages
          })          
        }
      })
    }else{
      console.log(this.data.comment)
      //查看本品
      axios({
        url: '/bask/baskList',
        data: {
          type: 1,   //1：本品晒单，2：全部晒单
          page: page,
          goodsId: this.data.goodsId,
          userid: this.data.userid,
          token: this.data.token
        }
      }).then(res => {
        console.log(res)
        //如果有数据
        if (res.data.data){
          if (!loadmore) {
            this.setData({
              titleindex: 1,
              data: res.data.data,
              totalpages: res.data.totalpages,
              oneNum: res.data.oneNum
            })
          } else {
            let data = this.data.data.concat(res.data.data)
            console.log(data)
            this.setData({
              titleindex: 1,
              data: data,
              totalpages: res.data.totalpages,
              oneNum: res.data.oneNum
            })
          }
        } else {
          this.setData({
            data: '',
            totalpages: '',
            oneNum: '',
            canload: false
          })
        }
      })
    }  
    if (this.data.page == this.data.totalpages) {
      this.setData({
        canload: false
      })
    }
  },
  //碰到底部
  onReachBottom: function () {
    let totalpages = this.data.totalpages
    let page = this.data.page
    //如果页码小于总页码 就是说还有数据的话
    if (page < totalpages){
      page ++
      this.setData({
        page: page,
        canload: true
      })
      this.getdata(this.data.titleindex, page,true)
    }else{
      this.setData({
        canload: false
      })
    }
  }
})